package com.zte.claa.inficombo.csa.app.util;

public class EdianUtil {
	// 小端字节序.
	public static void shortToByte_LH(short shortVal, byte[] b, int offset) {
		b[0 + offset] = (byte) (shortVal & 0xff);
		b[1 + offset] = (byte) (shortVal >> 8 & 0xff);
	}

	// 大端字节序.
	public static void shortToByte_HL(short shortVal, byte[] b, int offset) {
		b[0 + offset] = (byte) (shortVal >> 8 & 0xff);
		b[1 + offset] = (byte) (shortVal & 0xff);
	}

	// 小端字节序
	public static short byteToShort_LH(byte[] b, int offset) {
		return (short) ((((b[offset + 1]) << 8) & 0xff00 | b[offset] & 0x00ff));
	}
	
	// 大端字节序
	public static short byteToShort_HL(byte[] b, int offset) {
		return (short) ((((b[offset]) << 8) & 0xff00 | b[offset+1] & 0x00ff));
	}

	// 小端字节序
	public static void intToByte_LH(int intVal, byte[] b, int offset) {
		b[0 + offset] = (byte) (intVal & 0xff);
		b[1 + offset] = (byte) (intVal >> 8 & 0xff);
		b[2 + offset] = (byte) (intVal >> 16 & 0xff);
		b[3 + offset] = (byte) (intVal >> 24 & 0xff);
	}
	
	// 大端字节序
	public static void intToByte_HL(int intVal, byte[] b, int offset) {
		b[3 + offset] = (byte) (intVal & 0xff);
		b[2 + offset] = (byte) (intVal >> 8 & 0xff);
		b[1 + offset] = (byte) (intVal >> 16 & 0xff);
		b[0 + offset] = (byte) (intVal >> 24 & 0xff);
	}

	// 小端字节序
	public static int byteToInt_LH(byte[] b, int offset) {
		return (((b[3 + offset] & 0x00ff) << 24) & 0xff000000) 
				| (((b[2 + offset] & 0x00ff) << 16) & 0x00ff0000)
				| (((b[1 + offset] & 0x00ff) << 8) & 0x0000ff00) 
				| ((b[0 + offset] & 0x00ff));
	}
	
	// 小端字节序
	public static int byteToInt_HL(byte[] b, int offset) {
		return (((b[0 + offset] & 0x00ff) << 24) & 0xff000000) 
				| (((b[1 + offset] & 0x00ff) << 16) & 0x00ff0000)
				| (((b[2 + offset] & 0x00ff) << 8) & 0x0000ff00) 
				| ((b[3 + offset] & 0x00ff));
	}
}
